package com.ipharma.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.application.services.Batch;
import com.ipharma.dto.Task;

/**
 * A Swing-based dialog class.
 * <P>
 * @author JorgE.H
 */
public class GUIBatcher extends GUIDialog
{
  /**
   * The layout used by the main panel.
   */
  GridBagLayout layoutBase = new GridBagLayout();
//  GridLayout layoutQties = new GridLayout( 3, 3, 4, 4 );
  GridLayout layoutOptions = new GridLayout( 1, 2, 0, 0 );

  /**
   * The main panel.
   */
  JPanel jPanelBase = new JPanel();
  JPanel jPanelQties = new JPanel();
  JPanel jPanelOptions = new JPanel();
  JLabel jLabelOptions = new JLabel();
  JLabel jLabelTitle = new JLabel();
  JLabel jLabelQty = new JLabel();
  JLabel jLabelConf = new JLabel();
  GUIButtonImage jButtAcept;
  GUIButtonImage jButtCancel;
  JList jSelectList;
  JScrollPane jSelectPane = new JScrollPane();

  /**
   * Attributes
   */
  Batch batch;
  Batch[] batchs;
  String txtAction, txtConfirm;
  GUIOperation myContainer;
  Task task;

  /**
   * Constructs a new instance.
   * @param parent
   * @param title
   * @param modal
   */
  public GUIBatcher( GUIOperation pContainer, String title, boolean modal )
  {
    super( pContainer.myContainer.mcdMain.getFrame(), title, modal );
    try
    {
      myContainer = pContainer;
      txtAction = Texts.BCR_BATCHNUMBER;
      txtConfirm = Texts.BCR_CONFIRM;
      jbInit();
      pack();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    // sets panels
    jPanelBase.setLayout( layoutBase );
    jPanelBase.setPreferredSize( McdPrs.dialogSize );
//    jPanelQties.setLayout( layoutQties );
    jPanelOptions.setLayout( layoutOptions );
    jPanelBase.setBackground( Colors.QTR_BACK );
    jPanelQties.setBackground( Colors.QTR_BACK );

    // sets labels
    jLabelTitle.setText( "Product Name" );
    jLabelTitle.setFont( Texts.fntTitle );
    jLabelTitle.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelTitle.setForeground( Colors.APN_TEXT );
    jLabelQty.setText( txtAction );
    jLabelQty.setFont( Texts.fntText );
    jLabelQty.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelQty.setBorder( BorderFactory.createMatteBorder( 1, 0, 1, 0, Colors.QTR_BORDER ) );
    jLabelQty.setForeground( Colors.APN_TEXT );
    this.setBatch( "" );
    jLabelConf.setFont( Texts.fntText );
    jLabelConf.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelConf.setBorder( BorderFactory.createMatteBorder( 1, 0, 1, 0, Colors.QTR_BORDER ) );
    jLabelConf.setForeground( Colors.APN_TEXT );

    // batchs panel
    jPanelQties.add( jSelectPane, SwingConstants.CENTER );
    
    // option buttons
    jButtCancel = new GUIButtonImage( Images.buttCalcCancel, Images.buttCalcCancelDn );
    jButtCancel.addActionListener( new GUIBatcher_jButtCancel_actionAdapter( this ) );
    jButtAcept = new GUIButtonImage( Images.buttCalcAcept, Images.buttCalcAceptDn );
    jButtAcept.addActionListener( new GUIBatcher_jButtAcept_actionAdapter( this ) );

    getContentPane().add( jPanelBase );
    jPanelBase.add( jLabelTitle, new GridBagConstraints2( 0, 0, 2, 1, 1.0, 0.0,
        GridBagConstraints.NORTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jLabelQty, new GridBagConstraints2( 0, 1, 2, 1, 1.0, 0.0,
        GridBagConstraints.NORTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jPanelQties, new GridBagConstraints2( 0, 2, 2, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jLabelConf, new GridBagConstraints2( 0, 3, 2, 1, 1.0, 0.0,
        GridBagConstraints.SOUTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jButtCancel, new GridBagConstraints2( 0, 4, 1, 1, 1.0, 0.0,
        GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jButtAcept, new GridBagConstraints2( 1, 4, 1, 1, 1.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
  }

  /**
   * Sets selected product quantity
   * @param pQty quantity selected
   */
  public void setBatch( String pBatch )
  {
    this.jLabelConf.setText( txtConfirm + pBatch + Texts.CFR_FORSURE_END );
  }

  /**
   * Sets product for quantity selection
   * @param pProd Product instance selected
   * @param pAll indicates if buttons must be all active or only those permited by quantity
   */
  public void setTask( Task pTask )
  {
    task = pTask;

    try
    {
      // gets batch numbers for product
      batchs = myContainer.theDispenser.getProductBatchs( task.getProdId() );
      jSelectList = new JList( batchs );

      // Anonnimous class that performs mouse adapter for jList
      MouseListener mouseListener = new MouseAdapter() {
        public void mouseClicked( MouseEvent e )
        {
          int index = jSelectList.locationToIndex( e.getPoint() );
          jSelectList.setSelectedIndex( index );
          jSelectList.ensureIndexIsVisible( index );
          myContainer.myContainer.kickTimer();
          batch = (Batch) jSelectList.getSelectedValue();
          setBatch( batch.toString() );
        }
      };
      jSelectList.addMouseListener( mouseListener );
      jSelectList.setFont( Texts.fntList );
      jSelectList.setSelectionBackground( Colors.AST_SELECT );
      jSelectList.setRequestFocusEnabled( false );
//      jSelectPane.getViewport().removeAll();
      jSelectPane.getViewport().add( jSelectList, null );
      jSelectPane.getVerticalScrollBar().setPreferredSize( new Dimension( 40, 40 ) );
      jSelectList.repaint();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Returns selected value
   * @return String selected value
   */
  public String getSelection()
  {
    if ( batch != null )
      return "" + batch.getBatchNumber();
    else
      return "";
  }

  void jButtCancel_actionPerformed( ActionEvent e )
  {
    batch = null;
    this.stop();
  }

  void jButtAcept_actionPerformed( ActionEvent e )
  {
    this.stop();
  }

}

class GUIBatcher_jButtCancel_actionAdapter implements java.awt.event.ActionListener
{
  GUIBatcher adaptee;

  GUIBatcher_jButtCancel_actionAdapter( GUIBatcher adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtCancel_actionPerformed( e );
  }
}

class GUIBatcher_jButtAcept_actionAdapter implements java.awt.event.ActionListener
{
  GUIBatcher adaptee;

  GUIBatcher_jButtAcept_actionAdapter( GUIBatcher adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtAcept_actionPerformed( e );
  }
}

